#!/bin/sh

PROGNAME="$0"

set -e
#set -x

usage() {
	echo "usage: $PROGNAME FILENAME NUMBER"
	exit
}

get_duration() {
	ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1" | awk '{print int($1+0.5)}'
}

[ $# -lt 2 ] && usage

file="$1"
left="$2"

duration=$(get_duration "$file")
#echo "duration: $duration"

for n in $(seq 1 $left); do
	time=$(shuf -i 1-$duration -n 1)
	ffmpeg -y -nostats -loglevel error -ss $time -i "$file" -frames:v 1 -q:v 2 "${file}_$n.jpg" </dev/null || true
done
